using System;
using System.Collections.Generic;
using System.Text;

namespace Scheduler
{

    /*
     * The object of this class holds following information about a Therapy:
     * Its name (String theName), 
     * The room this therapy can be provided in(String room)  
     * priceList(a list of type TherapyPrice) that holds the prices for the
     * different durations this therapy may be offered in 
     * (LinkedList < TherapyPrice > priceList) 
     */

    public class Therapy
    {
        private String therName;
        private String therID;
        private int roomID;
        private LinkedList<TherapyPrice> priceList;

        public Therapy()
        {
            therName = "";
            therID = "";
            roomID = 0;
            priceList = new LinkedList<TherapyPrice>();

        }

        public Therapy(String therName)
        {
            this.therName = therName;
            this.therID = therName + "_0";
            this.roomID = 0;
            priceList = new LinkedList<TherapyPrice>();
        }

        public Therapy( String therName, int roomID)
        {
            this.therName = therName;
            this.therID = therName + "_" + roomID.ToString();
            this.roomID = roomID;
            priceList = new LinkedList<TherapyPrice>();
        }

        public Therapy (Database db, String rmtID, String therapyName) 
        {
            therName = therapyName;
            roomID = 0;
            therID = therapyName + "_" + roomID.ToString();
            priceList = db.readTherapyPriceList(rmtID, therID);
        }

        public String Name
        {
            get { return therName; }
            set { therName = String.Copy(value); }
        }

        public String TherapyID
        {
            get { return therID; }
            set { therID = value; }
        }

        public int RoomID
        {
            get { return roomID; }
            set { roomID = value; }
        }

        public LinkedList<TherapyPrice> PriceList
        {
            get { return priceList; }
            set { priceList = value; }
        }

        public String displayTherapyInfo()
        {
            String info = "";
            foreach (TherapyPrice curTherPrice in this.PriceList)
                info = info + curTherPrice.ToString();
            return "Therapy: " + this.Name + ":\r\r\n" + info;
        }

        public int findPrice(int duration)
        {
            foreach (TherapyPrice curTP in priceList)
            {
                if (curTP.Duration == duration)
                    return curTP.Price;
            }

            return -1;
        }

        public override string ToString()
        {
            String info = "";
            foreach (TherapyPrice curTherPrice in this.PriceList)
                info = info + curTherPrice.ToString();
            return "Therapy: " + this.Name + ":\r\r\n" + info;
        }

        public override bool Equals(object obj)
        {
            if (obj is Therapy)
                return (((Therapy)obj).Name == this.Name);
            else
                return false;
        }


        public void writeToDatabase (Database db, String id)
         {
             db.writeTherapy(this);
             foreach (TherapyPrice curTP in PriceList)
             {
                 curTP.writeToDatabase(db, id, this.therID );
             }
         }
        /*
         * Remove the therapy prices for this therapy from the database
         */
        public void removeFromDatabase(Database db, String id)
        {
            foreach (TherapyPrice curTP in PriceList)
            {
                curTP.removeFromDatabase(db, id, this.therID);   
            }
        }

        /* 
         * Reads the name of the all available RMTs from the database.
         */
        public static LinkedList<Therapy> readAllTherapies(Database db)
        {
            return db.readAllTherapies();
        }
    } 
}
